package com.xiaoxu.rental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xiaoxu.rental.entity.AutoInfo;
import com.xiaoxu.rental.mapper.AutoInfoMapper;
import com.xiaoxu.rental.service.AutoInfoService;
import com.xiaoxu.rental.service.OssService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;

/**
* @author xtq
* @description 针对表【auto_info】的数据库操作Service实现
* @createDate 2024-07-26 08:47:27
*/
@Service
@Transactional
public class AutoInfoServiceImpl extends ServiceImpl<AutoInfoMapper, AutoInfo>
    implements AutoInfoService {
    @Resource
    private OssService ossService;
    @Override
    public Page<AutoInfo> searchByPage(Page<AutoInfo> page, AutoInfo autoInfo) {
        return baseMapper.searchByPage(page, autoInfo);
    }

    @Override
    public Object delete(String ids) {
        List<Integer> list =
                Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        if (!list.isEmpty()){
            //同时删除图片
            list.forEach(id->{
                AutoInfo autoInfo = baseMapper.selectById(id);
                if (autoInfo!=null){
                    String pic = autoInfo.getPic();
                    if (pic!=null){
                        ossService.delete(pic);
                    }
                }
            });
            return baseMapper.deleteBatchIds(list)>0;

        }
        return false;

    }

    @Override
    public AutoInfo selectAutoNum(String autoNum) {
        QueryWrapper<AutoInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("auto_num",autoNum);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<AutoInfo> toBeMaintain() {
        return baseMapper.toBeMaintain();
    }

}




